package cn.tedu.weibo.controller;

import cn.tedu.weibo.common.response.JsonResult;
import cn.tedu.weibo.common.response.StatusCode;
import cn.tedu.weibo.pojo.dto.CommentDTO;
import cn.tedu.weibo.pojo.vo.CommentVO;
import cn.tedu.weibo.pojo.vo.UserVO;
import cn.tedu.weibo.service.ICommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "评论模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    @ApiOperation("发布评论")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        log.info("发布评论：{}", commentDTO);
        UserVO user = (UserVO) session.getAttribute("user");
        int res = commentService.insert(commentDTO,user.getId());
        if(res <= 0){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    @ApiOperation("获取评论列表")
    @GetMapping("selectByWeiboId")
    public JsonResult selectByWeiboId(
            @Range(min = 1,max = 1000,message = "微博id范围1-1000") @RequestParam Long id){
        List<CommentVO> list = commentService.selectByWeiboId(id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,list);
    }


}
